package org.kimoji.show.web;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.kimoji.entity.Album;
import org.kimoji.entity.Category;
import org.kimoji.entity.Item;
import org.kimoji.entity.biz.ItemBiz;
import org.kimoji.global.service.GlobalService;
import org.kimoji.show.service.ShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

@Controller
public class ShowController {
	
	@Autowired
	private ShowService showService;
	@Autowired
	private GlobalService globalService;
	
	/**
	 * 首頁
	 * <strong>Description</strong>:
	 * @param modelMap
	 * @return
	 * @return String
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="index")
	public String index(ModelMap modelMap){
		List<ItemBiz> items = showService.show(1,null, 1,null);
		modelMap.put("items", items);
		List<Category> categories = globalService.showCategory();
		modelMap.put("categories", categories);
		List<Item> hots = showService.hotResource();
		modelMap.put("hots", hots);
		List<Item> news = showService.newResource();
		modelMap.put("news", news);
		return "index";
	}
	
	/**
	 * <strong>Description</strong>:
	 * @param type 查詢類型:新番,專輯,求種
	 * @param category 類別:全部,熟女,幼齒……
	 * @param order 排序:最新(newest),最熱(hottest)
	 * @param index 查詢索引:0,20,40……
	 * @param modelMap
	 * @return
	 * @return Object
	 * @author ANGORO
	 * 2013-11-23
	 */
	@RequestMapping(value="{type}/{category}/{order}/{index}")
	@ResponseBody
	public Object show(@PathVariable String type,@PathVariable int category,@PathVariable String order,@PathVariable int index,String keyword,ModelMap modelMap){
		if("video".equals(type)){
			//新番
			return showService.show(category, order, index, keyword);
		}else if("album".equals(type)){
			//專輯
			return showService.showAlbum(category, order, index, keyword);
		}else{
			//求種
			return showService.showSeek(category, order, index, keyword);
		}
		
	}
	
	
	
	/**
	 * 彈窗
	 * <strong>Description</strong>:
	 * @param id
	 * @param modelMap
	 * @return
	 * @return String
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="dialog/{id}")
	public String dialog(@PathVariable String id,ModelMap modelMap){
		ItemBiz itemBiz = showService.showForDialog(id);
		modelMap.put("item", itemBiz);
		return "dialog";
	}
	
	/**
	 * 詳細頁
	 * <strong>Description</strong>:
	 * @param id
	 * @param modelMap
	 * @return
	 * @return String
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="detail/{id}")
	public String detail(@PathVariable String id,ModelMap modelMap){
		ItemBiz itemBiz = showService.showForDetail(id);
		modelMap.put("item", itemBiz);
		
		List<Item> relativeResource = showService.relativeResource(itemBiz);
		modelMap.put("relativeResource", relativeResource);
		
		List<Item> recommendResource = showService.recommendResource(itemBiz);
		modelMap.put("recommendResource", recommendResource);
		
		return "detail";
	}
	
	/**
	 * 專輯首頁
	 * <strong>Description</strong>:
	 * @param modelMap
	 * @return
	 * @return String
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="album")
	public String album(ModelMap modelMap){
		List<Album> albums = showService.showAlbum(1, null, 1,null);
		modelMap.put("albums", albums);
		List<Album> hotAlbums = showService.hotAlbum();
		modelMap.put("hotAlbums", hotAlbums);
		return "album/index";
	}
	
	/**
	 * 專輯詳細頁
	 * <strong>Description</strong>:
	 * @param id
	 * @param modelMap
	 * @return
	 * @return String
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="album/detail/{id}")
	public String albumDetail(@PathVariable int id,ModelMap modelMap){
		Album album = showService.getAlbum(id);
		modelMap.put("album", album);
		List<ItemBiz> items = showService.show(1, null, 1,null);
		modelMap.put("items", items);
		return "album/detail";
	}
	
	/**
	 * <strong>Description</strong>:
	 * @param id
	 * @param index
	 * @return
	 * @return Object
	 * @author chok
	 * 2014-2-2
	 */
	@RequestMapping(value="album/detail/{id}/{index}")
	@ResponseBody
	public Object albumShow(@PathVariable int id,@PathVariable int index){
		return showService.show(1, null, 1,null);
	}
}